package com.hongxun.design.utils.projectdatautil;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.acceptance.mode.ProblemR;
import com.hongxun.design.acceptance.mode.ProjectItem;
import com.hongxun.design.acceptance.mode.problemExist;
import com.hongxun.design.acceptance.mode.problemExistPhoto;
import com.hongxun.design.activitys.BaseActivity;
import com.hongxun.design.activitys.ProjectMapActivity;
import com.hongxun.design.bean.Exploration;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.bean.SearchProjectBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.ShardPreferencesTool;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.object_properties.MyMessage;
import com.hongxun.design.ui.dialogstyle.MyDialog;
import com.hongxun.design.utils.FragmentUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.fileutil.FileUtil;
import com.hongxun.design.utils.fileutil.SetFileUtil;
import com.hongxun.design.utils.httputil.HttpRootUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.hongxun.design.MyApp.activity;

/**
 * Created by zerdoor_pc on 2016/8/19.
 */
public class DataFactory {

    /**
     * 过滤项目阶段*
     */
    public static List<ProjectItemBean> GetNewProject(List<ProjectItemBean> nData, List<ProjectItemBean> oData) {
        if (nData == null || oData == null) return new ArrayList<>();
        List<ProjectItemBean> newData = new ArrayList<>();//新数据编辑模板
        List<ProjectItemBean> oldData = new ArrayList<>();//缓存数据编辑模板
        List<ProjectItemBean> currData = new ArrayList<>();//缓存数据编辑模板
        newData.addAll(nData);
        oldData.addAll(oData);
        for (int i = 0; i < nData.size(); i++) {
            for (int j = 0; j < oData.size(); j++) {
                if (nData.get(i) != null && oData.get(j) != null) {
                    if (!(nData.get(i).getStage_id().equals(MString.code_null) || nData.get(i).getStage_id().equals(MString.code_zero)) && nData.get(i).getStage_id().equals(oData.get(j).getStage_id())) {
                        //新数据stage_id覆盖
                        oldData.set(j, nData.get(i));
                        newData.remove(nData.get(i));
                        break;
                    } else if (nData.get(i).getUUID().length() > 0 && nData.get(i).getUUID().equals(oData.get(j).getUUID())) {
                        try {

                            if (!(nData.get(i).getStage_id().equals(MString.code_null) || nData.get(i).getStage_id().equals(MString.code_zero)) &&
                                    !(oData.get(i).getStage_id().equals(MString.code_null) || oData.get(i).getStage_id().equals(MString.code_zero))) {

                            } else if ((nData.get(i).getStage_id().equals(MString.code_null) || nData.get(i).getStage_id().equals(MString.code_zero)) &&
                                    (oData.get(i).getStage_id().equals(MString.code_null) || oData.get(i).getStage_id().equals(MString.code_zero))) {

                            } else if (!(nData.get(i).getStage_id().equals(MString.code_null) || nData.get(i).getStage_id().equals(MString.code_zero))) {
                                String stageid = nData.get(i).getStage_id();
                                MyApp.File.copyFolder(MyApp.File.getProRequestPath(oData.get(i).getUUID()), MyApp.File.getProRequestPath(stageid));
                            } else if (!(oData.get(i).getStage_id().equals(MString.code_null) || oData.get(i).getStage_id().equals(MString.code_zero))) {
                                String stageid = oData.get(i).getStage_id();
                                MyApp.File.copyFolder(MyApp.File.getProRequestPath(nData.get(i).getUUID()), MyApp.File.getProRequestPath(stageid));
                            }

                        } catch (Exception e) {
                        }
                        //同uuid数据覆盖
                        oldData.set(j, nData.get(i));
                        newData.remove(nData.get(i));
                        break;
                    }

                }
            }
        }

        oldData.addAll(0, newData);
        oData.clear();
        oData.addAll(oldData);
        for (ProjectItemBean p : oData) {//过滤离线创建的项目加到表头
            if (p != null) {
                if ((p.getStage_id().equals(MString.code_null) || p.getStage_id().equals(MString.code_zero)) && p.getUUID().length() > 0) {
                    currData.add(p);
                    oldData.remove(p);
                }
            }
        }
        oldData.addAll(0, currData);
        return oldData;

        //过滤规则：1、Stage_id是否为0，为0：判断uuid是否相等（相等：覆盖；不相等：跳过）；不为0：判断是否一致（一致：覆盖；不一致：跳过）。
        //项目点击规则：1、判断状态：在线：Stage_id为0-上传，Stage_id不为0-判断更新时间是否一致：一致进入，否则同步；离线：有缓存进入，没有提示。

//        for (ProjectItemBean n : nData) {
//            for (ProjectItemBean o : oData) {
//                if (n.getStage_id()!=null&&n.getStage_id().equals(o.getStage_id())) {
//                    break;
//                }
//            }
//        }
//        for (ProjectItemBean d : ndata) {
//            if (newData.contains(d)) {
//                newData.remove(d);//移除相同的
//            }
//        }
//        for (ProjectItemBean d : odata) {
//            if (oldData.contains(d)) {
//                oldData.remove(d);//移除相同的
//            }
//        }
//
//        for (ProjectItemBean n : newData) {
//            for (ProjectItemBean o : oldData) {
//                if (n.getUUID().equals(o.getUUID())) {
//                    ndata.add(n);//UUID相同的
//                    odata.add(o);//UUID相同的
//                    break;
//                }
//            }
//        }
//        for (ProjectItemBean d : ndata) {
//            if (newData.contains(d)) {
//                newData.remove(d); //移除相同的
//            }
//        }
//        for (ProjectItemBean d : odata) {
//            if (oldData.contains(d)) {
//                oldData.remove(d);//移除相同的
//            }
//        }
//        if (newData.size() > 0) {
//            ndata.addAll(0, newData);
//        }
//        if (oldData.size() > 0) {
//            ndata.addAll(0, oldData);
//        }
//        return ndata;
    }

    /**
     * 在线缓存过滤项目阶段*
     */
    public static List<ProjectItemBean> GetSnyProject(List<ProjectItemBean> nData, List<ProjectItemBean> oData) {
        if (nData == null || oData == null) return new ArrayList<>();
        List<ProjectItemBean> newData = new ArrayList<>();//新数据编辑模板
        List<ProjectItemBean> oldData = new ArrayList<>();//缓存数据编辑模板
        List<ProjectItemBean> currData = new ArrayList<>();//缓存数据编辑模板
        newData.addAll(nData);
        oldData.addAll(oData);
        for (int i = 0; i < nData.size(); i++) {
            for (int j = 0; j < oData.size(); j++) {

                if (!(nData.get(i).getStage_id().equals(MString.code_null) || nData.get(i).getStage_id().equals(MString.code_zero)) && nData.get(i).getStage_id().equals(oData.get(j).getStage_id())) {
                    //新数据stage_id覆盖
                    oldData.set(j, nData.get(i));
                    newData.remove(nData.get(i));
                    break;
                } else if (nData.get(i).getUUID().length() > 0 && nData.get(i).getUUID().equals(oData.get(j).getUUID())) {
                    if (!(nData.get(i).getStage_id().equals(MString.code_null) || nData.get(i).getStage_id().equals(MString.code_zero)) &&
                            !(oData.get(i).getStage_id().equals(MString.code_null) || oData.get(i).getStage_id().equals(MString.code_zero))) {

                    } else if ((nData.get(i).getStage_id().equals(MString.code_null) || nData.get(i).getStage_id().equals(MString.code_zero)) &&
                            (oData.get(i).getStage_id().equals(MString.code_null) || oData.get(i).getStage_id().equals(MString.code_zero))) {

                    } else if (!(nData.get(i).getStage_id().equals(MString.code_null) || nData.get(i).getStage_id().equals(MString.code_zero))) {
                        String stageid = nData.get(i).getStage_id();
                        MyApp.File.copyFolder(MyApp.File.getProRequestPath(oData.get(i).getUUID()), MyApp.File.getProRequestPath(stageid));
                    } else if (!(oData.get(i).getStage_id().equals(MString.code_null) || oData.get(i).getStage_id().equals(MString.code_zero))) {
                        String stageid = oData.get(i).getStage_id();
                        MyApp.File.copyFolder(MyApp.File.getProRequestPath(nData.get(i).getUUID()), MyApp.File.getProRequestPath(stageid));
                    }
                    //同uuid数据覆盖
                    oldData.set(j, nData.get(i));
                    newData.remove(nData.get(i));
                    break;
                }
            }
        }

        oldData.addAll(0, newData);
        oData.clear();
        oData.addAll(oldData);
        for (ProjectItemBean p : oData) {//过滤离线创建的项目加到表头
            try {

                if ((p.getStage_id().equals(MString.code_null) || p.getStage_id().equals(MString.code_zero)) && p.getUUID().length() > 0) {
                    currData.add(p);
                    oldData.remove(p);
                }
            } catch (Exception e) {
            }
        }
        oldData.addAll(0, currData);
        return oldData;

        //过滤规则：1、Stage_id是否为0，为0：判断uuid是否相等（相等：覆盖；不相等：跳过）；不为0：判断是否一致（一致：覆盖；不一致：跳过）。
        //项目点击规则：1、判断状态：在线：Stage_id为0-上传，Stage_id不为0-判断更新时间是否一致：一致进入，否则同步；离线：有缓存进入，没有提示。

//        for (ProjectItemBean n : nData) {
//            for (ProjectItemBean o : oData) {
//                if (n.getStage_id()!=null&&n.getStage_id().equals(o.getStage_id())) {
//                    break;
//                }
//            }
//        }
//        for (ProjectItemBean d : ndata) {
//            if (newData.contains(d)) {
//                newData.remove(d);//移除相同的
//            }
//        }
//        for (ProjectItemBean d : odata) {
//            if (oldData.contains(d)) {
//                oldData.remove(d);//移除相同的
//            }
//        }
//
//        for (ProjectItemBean n : newData) {
//            for (ProjectItemBean o : oldData) {
//                if (n.getUUID().equals(o.getUUID())) {
//                    ndata.add(n);//UUID相同的
//                    odata.add(o);//UUID相同的
//                    break;
//                }
//            }
//        }
//        for (ProjectItemBean d : ndata) {
//            if (newData.contains(d)) {
//                newData.remove(d); //移除相同的
//            }
//        }
//        for (ProjectItemBean d : odata) {
//            if (oldData.contains(d)) {
//                oldData.remove(d);//移除相同的
//            }
//        }
//        if (newData.size() > 0) {
//            ndata.addAll(0, newData);
//        }
//        if (oldData.size() > 0) {
//            ndata.addAll(0, oldData);
//        }
//        return ndata;
    }

    /**
     * 过滤项目*
     */
    public static List<SearchProjectBean> GetProject(List<SearchProjectBean> nData, List<SearchProjectBean> oData) {

//        List<SearchProjectBean> ndata = new ArrayList<>();
//        List<SearchProjectBean> odata = new ArrayList<>();
        List<SearchProjectBean> newData = new ArrayList<>();
        List<SearchProjectBean> oldData = new ArrayList<>();
        newData.addAll(nData);
        oldData.addAll(oData);


        for (int i = 0; i < nData.size(); i++) {
            for (int j = 0; j < oData.size(); j++) {
                if (!(nData.get(i).getProject_id().equals(MString.code_null) || nData.get(i).getProject_id().equals(MString.code_zero)) && nData.get(i).getProject_id().equals(oData.get(j).getProject_id())) {
                    oldData.set(j, nData.get(i));
                    newData.remove(nData.get(i));
                    break;
                } else if (nData.get(i).getUUID().length() > 0 && nData.get(i).getUUID().equals(oData.get(j).getUUID())) {
                    oldData.set(j, nData.get(i));
                    newData.remove(nData.get(i));
                    break;
                }
            }
        }
        oldData.addAll(0, newData);
        return oldData;


//        for (SearchProjectBean n : newData) {
//            for (SearchProjectBean o : oldData) {
//                if (n.getProject_id().equals(o.getProject_id())) {
//                    ndata.add(n);//id相同的
//                    odata.add(o);//id相同的
//                    break;
//                }
//            }
//        }
//        for (SearchProjectBean d : ndata) {
//            if (newData.contains(d)) {
//                newData.remove(d);//移除相同的
//            }
//        }
//        for (SearchProjectBean d : odata) {
//            if (oldData.contains(d)) {
//                oldData.remove(d);//移除相同的
//            }
//        }
//
//        for (SearchProjectBean n : newData) {
//            for (SearchProjectBean o : oldData) {
//                if (n.getUUID()!=null&&n.getUUID().length()>0&&n.getUUID().equals(o.getUUID())) {
//                    ndata.add(n);//UUID相同的
//                    odata.add(o);//UUID相同的
//                    break;
//                }
//            }
//        }
//        for (SearchProjectBean d : ndata) {
//            if (newData.contains(d)) {
//                newData.remove(d); //移除相同的
//            }
//        }
//        for (SearchProjectBean d : odata) {
//            if (oldData.contains(d)) {
//                oldData.remove(d);//移除相同的
//            }
//        }
//        if (newData.size() > 0) {
//            ndata.addAll(newData);
//        }
//        if (oldData.size() > 0) {
//            ndata.addAll(oldData);
//        }
//        return ndata;
    }


    /**
     * 判断是否有同步缓存*
     */
    public static boolean check_rcache(Context context, String path) {
        String PATH = context.getFilesDir().getPath() + File.separator + MyApp.openIdent + File.separator + path;
        File f = new File(PATH);
        return FileUtil.checkFile(f);
    }

    public static boolean checkUserid(ProjectItemBean itemBean) {
        return true;
//        String userid=MyApp.userInfo.getUser_id();
//        if (itemBean.getAuditor().contains(userid)||itemBean.getDesigner().contains(userid)
//                ||itemBean.getExplorationer().contains(userid)||itemBean.getExplorationer_name().contains(userid)||itemBean.getManager().contains(userid)||itemBean.getManager_name().contains(userid)){
//            return true;
//        }else{
//            return false;
//        }

    }

    public static boolean check_cache(Context context, String path) {
        String PATH = context.getFilesDir().getPath() + File.separator + MyApp.openIdent + File.separator + path;
        File f = new File(PATH);

        return f.exists();
    }

    public static void ProjectClickMap(Activity activity, ProjectItemBean itemBean) {
        addLoadHandler();
        String nameOnLine = MString.Project_ + itemBean.getStage_id();
        String nameOutLine = MString.Project_ + itemBean.getUUID();
        boolean hascache = false;
        boolean isoutline = false;
        //判断有无缓存
        if (DataFactory.check_cache(activity, nameOnLine)) {
            hascache = true;//在线项目有缓存
        } else if ((itemBean.getStage_id().equals(MString.code_null) || itemBean.getStage_id().equals(MString.code_zero)) && (itemBean.getUUID().length() > 0 && DataFactory.check_cache(activity, nameOutLine))) {
            hascache = true;//离线项目有缓存
            isoutline = true;//离线标识
        } else {
            hascache = false;//项目无缓存
        }
        MyLog.L(MString.SUCCESSED + " itemBean.getStage_id()=" + itemBean.getStage_id());
        MyLog.L(MString.SUCCESSED + " itemBean.getUUID()=" + MyApp.isOnLine);
        MyLog.L(MString.SUCCESSED + " MyApp.isOnLine=" + MyApp.isOnLine);
        MyLog.L(MString.SUCCESSED + " hascache=" + hascache);
        MyLog.L(MString.SUCCESSED + " isoutline=" + isoutline);
        MyLog.L(MString.SUCCESSED + " DataFactory.getUpdate()=" + DataFactory.getUpdate());
        if (MyApp.isOnLine) {
//            if ((hascache && DataFactory.getUpdate())||isoutline) {//有缓存且同步标识为真或为离线项目
//                //判断是否同步
//                MyDialog.dialogForupdatePro(activity, itemBean);//选择是否同步
//            } else  {
////                无缓存直接进入
//                IntentToActivity.intent(activity, ProjectMapActivity.class);
//                        }
            String path = MString.Request + File.separator + MString.Project_ + MString.getValueId(MyApp.currProject.getStage_id(), MyApp.currProject.getUUID());
            if (DataFactory.check_rcache(activity, path)) {
                MyDialog.dialogForupdatePro(activity, MyApp.currProject);//选择是否同步
            } else {
                if (!MyApp.currProject.getUUID().equals(SetFileUtil.CheckStageId())) {
                    MyApp.HTTP.getload(handler, HttpStringUtil.SUCCESSED, SetFileUtil.CheckStageId());
                } else {
                    ToastShow.showShort(activity, "项目列表数据异常,请转到离线操作此项目！");
                }
            }

        } else {
            //直接进行
            if (isoutline || (hascache && !itemBean.getFlag().equals(MString.del))) {//有缓存或为离线项目或项目未删除
                IntentToActivity.intent(activity, ProjectMapActivity.class);
            } else if (itemBean.getFlag().equals(MString.del)) {
                ToastShow.showShort(activity, "此项目已删除！");
            } else {
                String name = MString.Project_ + MString.getValueId(itemBean.getStage_id(), itemBean.getUUID());
                MyApp.File.createFile(name);//项目文件夹
                MyApp.File.createFile(MString.Request + File.separator + name);//同步文件夹
                IntentToActivity.intent(activity, ProjectMapActivity.class);
            }
        }
    }

    //项目左滑同步
    public static void ProjectClick(final BaseActivity activity, ProjectItemBean itemBean) {
        BaseActivity.showWaitDialog(-1);
        addLoadHandler();
        MyApp.currProject.setBean(itemBean);
        String nameOnLine = MString.Project_ + itemBean.getStage_id();
        String nameOutLine = MString.Project_ + itemBean.getUUID();
        boolean hascache = false;
        boolean isoutline = false;
        MyApp.ReadOnly = checkIsOnlyRead(itemBean);
        //判断有无缓存
        if (DataFactory.check_cache(activity, nameOnLine)) {
            hascache = true;//在线项目有缓存
        } else if ((itemBean.getStage_id().equals(MString.code_null)
                || itemBean.getStage_id().equals(MString.code_zero))
                && (itemBean.getUUID().length() > 0
                && DataFactory.check_cache(activity, nameOutLine))) {
            hascache = true;//离线项目有缓存
            isoutline = true;//离线标识
        } else {
            hascache = false;//项目无缓存
        }
        if (MyApp.isOnLine) {
            String path = MString.Request + File.separator + MString.Project_ + MString.getValueId(MyApp.currProject.getStage_id(), MyApp.currProject.getUUID());
            if (DataFactory.check_rcache(activity, path)) {
                BaseActivity.hideWaitIngDialog();
                MyDialog.dialogForupdatePro(activity, MyApp.currProject);//选择是否同步
            } else {
                if (!MyApp.currProject.getUUID().equals(SetFileUtil.CheckStageId())) {
                    String read = ShardPreferencesTool.read(MyApp.context, MyApp.currProject.getProject_id(), "", 3);
                    List<Exploration> listEP = JSON.parseArray(read, Exploration.class);
                    if (listEP != null && !listEP.isEmpty()) {
                        String toJson = MyDialog.upLoadPointMaterials();
                        if (toJson == null) {
                            handler.sendEmptyMessage(5);
                        } else {
                            MyApp.HTTP.upPointOfflineData(handler, 5, toJson);
                        }
                    } else {
                        MyApp.HTTP.getload(handler, HttpStringUtil.SUCCESSED, SetFileUtil.CheckStageId());
                    }
                } else {
                    BaseActivity.hideWaitIngDialog();
                    ToastShow.showShort(activity, "项目数据异常,请转到离线操作此项目！");
                }
            }
        } else {
            //直接进行
            BaseActivity.hideWaitIngDialog();
            ToastShow.showShort(activity, "离线不能同步");
            if (isoutline || (hascache && !itemBean.getFlag().equals(MString.del))) {//有缓存或为离线项目或项目未删除
                IntentToActivity.intent(activity, ProjectMapActivity.class);
            } else if (itemBean.getFlag().equals(MString.del)) {
                ToastShow.showShort(activity, "此项目已删除！");
            } else {
                String name = MString.Project_ + MString.getValueId(itemBean.getStage_id(), itemBean.getUUID());
                MyApp.File.createFile(name);//项目文件夹
                MyApp.File.createFile(MString.Request + File.separator + name);//同步文件夹
                IntentToActivity.intent(activity, ProjectMapActivity.class);
            }
        }
    }

    public static void ProjectItemClick(final BaseActivity activity, ProjectItemBean itemBean) {
        BaseActivity.showWaitDialog(-1);
        addLoadHandler();
        MyApp.currProject.setBean(itemBean);
        MyApp.ReadOnly = checkIsOnlyRead(itemBean);
        BaseActivity.hideWaitIngDialog();
        if (MyApp.isOnLine) {
            IntentToActivity.intent(activity, ProjectMapActivity.class);
        } else {
            String name = MString.Project_ + MString.getValueId(itemBean.getStage_id(), itemBean.getUUID());
            MyApp.File.createFile(name);//项目文件夹
            MyApp.File.createFile(MString.Request + File.separator + name);//同步文件夹
            IntentToActivity.intent(activity, ProjectMapActivity.class);
        }
    }

    //项目单击同步
    public static Handler ProjectloadClick(Activity activity, ProjectItemBean itemBean, Handler handler) {
        addLoadHandler(handler);
        MyApp.currProject.setBean(itemBean);
        String path = MString.Project_ + MString.getValueId(MyApp.currProject.getStage_id(), MyApp.currProject.getUUID());
        String path2 = MString.Request + File.separator + path;
        if (DataFactory.check_rcache(activity, path)) {
            if (DataFactory.check_rcache(activity, path2)) {
                MyDialog.dialogForupdatePro(activity, MyApp.currProject);//选择是否同步
            } else {
                ToastShow.showShort(activity, "此项目没有更新！");
            }
        } else {
            ToastShow.showShort(activity, "此项目没有缓存！");
        }
        return handler;
    }


    private static void addLoadHandler(final Handler... sHandler) {
        handler = new Handler() {
            String p = "";
            boolean flag = false;

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (activity == null) return;
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED://0
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            String path = FastJsonUtil.JsonToZipPathBean((String) msg.obj);
                            flag = true;
                            if (path.length() > 0) {
                                p = MyApp.File.getCachaPath();
                                HttpRootUtil.dowloadFlag = false;
                                MyApp.File.copyFolderPhotos(p + MString.Project_ + SetFileUtil.CheckStageId_type(), MyApp.File.getCachaPath() + MString.Photos);
                                MyApp.HTTP.download(path, p, MString.Project_ + SetFileUtil.CheckStageId_type(), ".zip", handler);
                            }

                        } else {
                            String error = FastJsonUtil.JsonToCheck_String((String) msg.obj);
                            if (error.equalsIgnoreCase("internal server error") || (error.equalsIgnoreCase("internal") && error.equalsIgnoreCase("server") && error.equalsIgnoreCase("error"))) {
                                SetFileUtil.delectProjectCacheFile(MyApp.currProject);
                                ToastShow.showShort(activity, "此项目已被删除！");
                            } else {
                                ToastShow.showShort(activity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                            }
                            flag = false;
                            BaseActivity.hideWaitIngDialog();
                            //handler.sendEmptyMessage(2);
                        }
                        break;
                    case 1:
                        p = MyApp.File.getCachaPath() + MString.Project_ + SetFileUtil.CheckStageId_type();
                        // MyApp.File.delect(p2);//删除缓存文件夹
                        MyApp.File.delect(p + ".zip");//删除文件夹压缩包
                        DataFactory.setUpdateOn(false);
                        handler.sendEmptyMessage(2);
                        break;
                    case 2:
                        if (sHandler == null || sHandler.length == 0) {
//                            IntentToActivity.intent(activity, ProjectMapActivity.class);
                            ToastShow.showShort(activity, "同步完成");
                            //
                        } else {
                            MyLog.L(MString.SUCCESSED + "flag=" + flag);
                            sHandler[0].sendMessage(MyMessage.getMessage(Constant.NETLOAD, flag + ""));
                        }
                        break;
                    case 3:
                        break;
                    case HttpStringUtil.ERROR://99
                        BaseActivity.hideWaitIngDialog();
                        ToastShow.showShort(activity, (String) msg.obj);
                        if ("Internal Server Error".equalsIgnoreCase((String) msg.obj)) {
                            SetFileUtil.delectProjectCacheFile(MyApp.currProject);
                            ToastShow.showShort(activity, "此项目已被删除！");
                            FragmentUtil.getIntence(activity).homeFragment.Refresh();
                        }
                        break;
                    case 444://项目问题及其照片
                        Context context = activity.getApplicationContext();
                        //标记
                        ShardPreferencesTool.save(context, MyApp.currProject.getProject_id(), MString.xmYXZ, 1);
                        //下载数据
                        ProjectItem item = JSON.parseObject(String.valueOf(msg.obj), ProjectItem.class);
                        List<problemExist> exists = item.getProblemExistList();
                        List<problemExistPhoto> photos = item.getProblemExistPhotoList();

                        List<problemExist> exists0 = new ArrayList<>();
                        List<problemExistPhoto> photos0 = new ArrayList<>();

                        for (problemExist exist : exists) {
                            if (!exist.getStates().equals("删除")) {
                                exist.setStates(MString.xmYTB);
                                exists0.add(exist);
                            }
                        }
                        for (problemExistPhoto photo : photos) {
                            if (!photo.getStates().equals("删除")) {
                                photo.setStates(MString.xmYTB);
                                photos0.add(photo);
                            }
                        }
                        //由于先同步了，所以下载下来的数据时全部数据；包含删除状态的
                        saveExPro(context, FastJsonUtil.beanToJson(exists0));
                        saveExProPh(context, FastJsonUtil.beanToJson(photos0));
                        break;
                    case 5:
                        MyDialog.clearPointMaterials();
                        MyApp.HTTP.getload(handler, HttpStringUtil.SUCCESSED, SetFileUtil.CheckStageId());
                        break;
                }
            }
        };

    }

    public static Handler handler;


    /**
     * 设置更新时间*
     */
    public static void setUpdate(boolean f) {
        if (!MyApp.isOnLine) {
            if (activity != null && MyApp.currProject != null) {
                if (!(MyApp.currProject.getStage_id().equals(MString.code_null) || MyApp.currProject.getStage_id().equals(MString.code_zero))) {
                    ShardPreferencesTool.saveshare(activity, MString.Project_ + MyApp.currProject.getProject_id(), f);
                } else {
                    ShardPreferencesTool.saveshare(activity, MString.Project_ + MyApp.currProject.getUUID(), f);
                }
            }
        }

    }

    public static void setUpdateOn(boolean f) {
        if (activity != null && MyApp.currProject != null) {
            if (!(MyApp.currProject.getStage_id().equals(MString.code_null) || MyApp.currProject.getStage_id().equals(MString.code_zero))) {
                ShardPreferencesTool.saveshare(activity, MString.Project_ + MyApp.currProject.getProject_id(), f);
            } else {
                ShardPreferencesTool.saveshare(activity, MString.Project_ + MyApp.currProject.getUUID(), f);
            }
        }

    }

    /**
     * 获取更新 *
     */
    public static boolean getUpdate() {
        boolean s = false;
        if (activity != null && MyApp.currProject != null) {
            if (!(MyApp.currProject.getStage_id().equals(MString.code_null) || MyApp.currProject.getStage_id().equals(MString.code_zero))) {
                s = ShardPreferencesTool.getshare(activity, MString.Project_ + MyApp.currProject.getProject_id(), false);
            } else {
                s = ShardPreferencesTool.getshare(activity, MString.Project_ + MyApp.currProject.getUUID(), false);
            }

        }
        return s;
    }


    /**
     * 判断权限(false为无限制 true为只读权限)
     **/
    public static boolean checkIsOnlyRead(ProjectItemBean b) {

        if (b.getCurrent_schedule() == null || b.getCurrent_schedule().length() == 0) {
            return false;
        } else {
            int schedule = Integer.parseInt(b.getCurrent_schedule());
            if (schedule <= 11) {
                return false;
            } else {
                String M = MyApp.userInfo.getUser_id();
                if (schedule > 11 && b.getExplorationer().contains(M) && !b.getManager().contains(M) && !b.getAuditor().contains(M) && !b.getDesigner().contains(M)) {
                    //只是踏勘人员
                    return true;
                } else {
                    if (schedule == 12) {
                        return false;//除踏勘人员外都可操作数据
                    } else {
                        return (schedule > 12);//所有人员都不能操作数据
                    }
                }

            }
        }
    }

    /**
     * 获取编号
     **/
    public static String getAddNumber(String head) {
        String id = "", uuid = "";
        if (MyApp.currProject.getStage_id() != null && MyApp.currProject.getStage_id().length() > 0 && !MyApp.currProject.getStage_id().equalsIgnoreCase(MString.code_zero)) {
            id = ShardPreferencesTool.getshare(activity, getId(MyApp.currProject.getStage_id()) + head, "");
        } else if (MyApp.currProject.getUUID() != null && MyApp.currProject.getUUID().length() > 0 && !MyApp.currProject.getUUID().equalsIgnoreCase(MString.code_zero)) {
            uuid = ShardPreferencesTool.getshare(activity, getId(MyApp.currProject.getUUID()) + head, "");
        }
        if (id.length() == 0 && uuid.length() == 0) {
            return head + "-" + "0";
        } else if (id.length() == 0 && uuid.length() > 0) {
            if (MyApp.currProject.getStage_id().length() > 0 && !MyApp.currProject.getStage_id().equals(MString.code_zero)) {
                ShardPreferencesTool.saveshare(activity, getId(MyApp.currProject.getStage_id()) + head, uuid);
            }
            return uuid;
        } else {

            return id;
        }

    }


    public static String getStatid(String head) {
        return ShardPreferencesTool.getshare(activity, DataFactory.getId(MyApp.currProject.getStage_id()) + head + "_id", "");

    }

    public static String getUUID(String head) {
        return ShardPreferencesTool.getshare(activity, DataFactory.getId(MyApp.currProject.getUUID()) + head + "_id", "");

    }

    public static String getId(String id) {
        return id + "_" + MyApp.userInfo.getOpenIdent() + "_" + MyApp.currExploration.getExploration_type() + "_";
    }

    //读取问题
    public static List<problemExist> getExPro(Object... obj) {
        String readEx = ShardPreferencesTool.read(obj[0], MString.ExPro, "", 2);
        List<problemExist> exPro = JSON.parseArray(readEx, problemExist.class);
        if (exPro == null) exPro = new ArrayList<>();
        return exPro;
    }

    //保存问题
    public static void saveExPro(Object... obj) {
        ShardPreferencesTool.save(obj[0], MString.ExPro, obj[1], 2);
    }

    //保存问题照片
    public static void saveExProPh(Object... obj) {
        ShardPreferencesTool.save(obj[0], MString.ExProPhoto, obj[1], 2);
    }

    //问题照片
    public static List<problemExistPhoto> getExProPh(Object... obj) {
        String readEx = ShardPreferencesTool.read(obj[0], MString.ExProPhoto, "", 2);
        List<problemExistPhoto> photos = JSON.parseArray(readEx, problemExistPhoto.class);
        if (photos == null) photos = new ArrayList<>();
        return photos;
    }

    //读取关系
    public static List<ProblemR> getProblemRs(Object... obj) {
        String gxb = ShardPreferencesTool.read(obj[0], MString.lib_point, "", 1);
        List<ProblemR> rList = JSON.parseArray(gxb, ProblemR.class);
        if (rList == null) rList = new ArrayList<>();
        return rList;
    }

}

